package com.example.wallpaperapp.m;

import android.util.Log;

import com.example.wallpaperapp.bean.UserInfo;
import com.example.wallpaperapp.ihttps.RetrofitService;
import com.example.wallpaperapp.ui.activity.LoginActivity;
import com.example.wallpaperapp.v.LoginV;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @Package: com.example.wallpaperapp.m
 * @ClassName: LoginModel
 * @Description: java类作用描述
 * @Author: zrmo
 * @CreateDate: 2020/11/14 15:36
 */

public class LoginModel implements LoginM {
    @Override
    public void requestMsg(int name, String pwd,final callBack call) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.1.113:8080/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // 支持RxJava
                .build();
        RetrofitService service = retrofit.create(RetrofitService.class);
        Observable<List<UserInfo>> observable = service.getList();
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<UserInfo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d("TAG", "开始采用subscribe连接");
                    }
                    @Override
                    public void onNext(List<UserInfo> value) {
                        for (int i = 0; i < value.size(); i++) {
                            if (value.get(i).getPhone_number() == name && value.get(i).getPassword().equals(pwd)) {
                                call.onSuccess(value.get(i).getUser_id());
                                break;
                            }else {
                                call.onFailed("账号或者密码有误");
                            }
                        }
                        Log.d("TAG", "onNext: " + value.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        call.onFailed(e.getMessage());
                        Log.d("TAG", "对Error事件作出响应" + e.getMessage());
                    }
                    @Override
                    public void onComplete() {
                        Log.d("TAG", "对Complete事件作出响应");
                    }
                });

    }
}
